<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	set_html('title','申请成为红娘');
	$return['worker']=$this->get('worker',array('mid'=>$_SESSION['uid']));
	$return['post']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	$groups=pdo_getall('jy_xiangqin_fuwu',array('uniacid'=>$_SESSION['uniacid']),'','id');
	$groups[0]=array('id'=>0,'title'=>'无服务中心');
	$return['groups']=$groups;
	$return['applyset']=$this->plugin_setting('redapply');
	$return['post']['diyfields']=$return['applyset']['diyformfields']?:array();
	exi($return);
}
if($_RQ['op']=='apply'){
	$post=get_postdata();
	$return['applyset']=$this->plugin_setting('workersetting');
	if($_SESSION['container']!='wxapp' && $return['applyset']['is_smscode'] && !check_sms_code($post['smscode'])){
		exi('手机验证码错误！','error');
	}
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	$data=array(
		'title'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
		'wechat'=>trim($post['wechat']),
		'fuwuid'=>intval($post['fuwuid']),
		'diyfields'=>ijson_encode($post['diyfields']),
		'status'=>0,
	);
	$pid=$this->model('worker')->register($data);
	$this->model('worker')->apply_agent($pid);
	exi('申请成功！请耐心等待管理员审核！','','jy_xiangqin/worker.index/');
}